import { Injectable, Inject, BadRequestException } from '@nestjs/common'
import { CreateModeDto } from './dto/create-mode.dto'
import { UpdateModeDto } from './dto/update-mode.dto'
import { Repository } from 'typeorm'
import { Mode } from './entities/mode.entity'

@Injectable()
export class ModeService {
  constructor(
    @Inject('MODE_REPOSITORY')
    private readonly modeRepository: Repository<Mode>
  ) { }

  async create(createModeDto: CreateModeDto) {
    let res = undefined
    try {
      res = await this.modeRepository.insert(createModeDto)
    } catch (error) {
      throw new BadRequestException("插入模式数据错误：" + error);
    }
    return res;
  }

  async findAll() {
    let res = undefined
    try {
      res = await this.modeRepository.findAndCount()
    } catch (error) {
      throw new BadRequestException("查找模式数据错误：" + error);
    }
    return res;
  }

  async findOne(id: number) {
    let res = undefined
    try {
      res = await this.modeRepository.findOne({ mid: id })
    } catch (error) {
      throw new BadRequestException("查找模式数据错误：" + error);
    }
    return res;
  }

  async update(id: number, updateModeDto: UpdateModeDto) {
    let res = undefined
    try {
      res = await this.modeRepository.update({ mid: id }, updateModeDto)
    } catch (error) {
      throw new BadRequestException("更新模式数据错误：" + error);
    }
    return res;
  }

  async remove(id: number) {
    let res = undefined
    try {
      res = await this.modeRepository.delete({ mid: id })
    } catch (error) {
      throw new BadRequestException("删除模式数据错误：" + error);
    }
    return res;
  }
}
